package com.swak.event;

import java.util.concurrent.CompletableFuture;

public abstract class AbstractEventHandler<E> implements EventHandler<E> {

	protected EventHandler<E> handler;

	@Override
	public CompletableFuture<Boolean> doHandler(E event) {
		CompletableFuture<Boolean> future = this.support(event) ? this.doInnerHandler(event) : null;
		if (future != null) {
			return future;
		} else if (handler != null) {
			return handler.doHandler(event);
		}
		return CompletableFuture.completedFuture(null);
	}

	@Override
	public EventHandler<E> next(EventHandler<E> handler) {
		this.handler = handler;
		return handler;
	}

	/**
	 * 是否支持此类型的事件
	 * 
	 * @param event
	 * @return
	 */
	protected abstract boolean support(E event);

	/**
	 * 子类需要实现的处理器
	 * 
	 * @param event
	 * @return
	 */
	protected abstract CompletableFuture<Boolean> doInnerHandler(E event);
}
